package com.yuji.aacar.controller.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;

import com.yuji.aacar.R;
import com.yuji.aacar.base.EventConst;
import com.yuji.aacar.base.UserCenter;
import com.yuji.aacar.bean.beans.QueryCarAddressInfo;
import com.yuji.aacar.bean.response.LoginResponse;
import com.yuji.aacar.core.cores.event.CoreEventCenter;
import com.yuji.aacar.widgets.autolayout.AutoLinearLayout;
import com.yuji.aacar.widgets.textview.PingFangTextView;

import java.util.ArrayList;


/**
 * <li>Package:com.yuji.aacar.controller.adapter</li>
 * <li>Author: lining </li>
 * <li>Date: 2018/3/2</li>
 * <li>Description:   </li>
 */

public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.ViewHolder> {

    protected Context mContext;
    protected ArrayList<QueryCarAddressInfo> mDatas;
    protected LayoutInflater mInflater;
    private LoginResponse user;

    public AddressAdapter(Context mContext, ArrayList<QueryCarAddressInfo> mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        mInflater = LayoutInflater.from(mContext);
    }

    public ArrayList<QueryCarAddressInfo> getDatas() {
        return mDatas;
    }

    public AddressAdapter setDatas(ArrayList<QueryCarAddressInfo> datas) {
        mDatas = datas;
        return this;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.activity_address_item, parent, false));
    }

    @Override
    public void onBindViewHolder(AddressAdapter.ViewHolder holder, int position) {
        QueryCarAddressInfo bean = mDatas.get(position);
        holder.name.setText(bean.getUserName());
        holder.telphone.setText(bean.getPhoneNo());
        if(TextUtils.isEmpty(bean.getAddrType())){
            holder.taglayout.setVisibility(View.INVISIBLE);
        }else{
            holder.taglayout.setVisibility(View.VISIBLE);
            holder.tag.setText(bean.getAddrType());
        }

        holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginResponse user = UserCenter.getCurrentUser();
                if (null != user) {
                    String userId = user.getMemberId();
                    UserCenter.setDefaultAddress(userId, bean);
                    CoreEventCenter.postObject(EventConst.SELECT_ADDRESS_OVER, bean);
                }
            }
        });

        holder.address.setText(bean.getCityName()+" "+bean.getAreaName()+" "+bean.getRoomName());
        if (bean.getIsDefault().contentEquals("1")){
            holder.setDefault.setChecked(true);
        } else {
            holder.setDefault.setChecked(false);
        }
        holder.setDefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RadioButton button = (RadioButton)v;
                if(button.isChecked()){
                    holder.setDefault.setChecked(false);
                    CoreEventCenter.postMessage(EventConst.CAR_MANAGE_DEFAULT, position+":"+ 0);
                } else {
                    holder.setDefault.setChecked(true);
                    CoreEventCenter.postMessage(EventConst.CAR_MANAGE_DEFAULT, position+":"+ 1);
                }

            }
        });
        holder.setDefaultTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.setDefault.isChecked()){
                    holder.setDefault.setChecked(false);
                    CoreEventCenter.postMessage(EventConst.CAR_MANAGE_DEFAULT, position+":"+ 0);
                } else {
                    holder.setDefault.setChecked(true);
                    CoreEventCenter.postMessage(EventConst.CAR_MANAGE_DEFAULT, position+":"+ 1);
                }
            }
        });
        holder.editImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CoreEventCenter.postMessage(EventConst.CAR_MANAGE_EDIT, position+"");
            }
        });
        holder.edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CoreEventCenter.postMessage(EventConst.CAR_MANAGE_EDIT, position+"");
            }
        });
        holder.deleteImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CoreEventCenter.postMessage(EventConst.CAR_MANAGE_DELETE, position+"");
            }
        });
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                CoreEventCenter.postMessage(EventConst.CAR_MANAGE_DELETE, position+"");
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas != null ? mDatas.size() : 0;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        AutoLinearLayout layout;
        PingFangTextView name;
        PingFangTextView telphone;
        AutoLinearLayout taglayout;
        PingFangTextView tag;
        PingFangTextView address;
        RadioButton setDefault;
        PingFangTextView setDefaultTv;
        ImageView editImage;
        PingFangTextView edit;
        ImageView deleteImage;
        PingFangTextView delete;

        public ViewHolder(View itemView) {
            super(itemView);
            layout = (AutoLinearLayout) itemView.findViewById(R.id.address_layout);
            name = (PingFangTextView) itemView.findViewById(R.id.address_name_tv);
            telphone = (PingFangTextView) itemView.findViewById(R.id.address_phone_tv);
            taglayout = (AutoLinearLayout) itemView.findViewById(R.id.address_tag_name_alyout);
            tag = (PingFangTextView) itemView.findViewById(R.id.address_tag_name_tv);
            address = (PingFangTextView) itemView.findViewById(R.id.address_address_tv);
            setDefault = (RadioButton) itemView.findViewById(R.id.address_radio_default);
            setDefaultTv = (PingFangTextView) itemView.findViewById(R.id.address_default_tv);
            editImage = (ImageView) itemView.findViewById(R.id.address_radio_edit);
            edit = (PingFangTextView) itemView.findViewById(R.id.address_edit_tv);
            deleteImage = (ImageView) itemView.findViewById(R.id.address_radio_delete);
            delete = (PingFangTextView) itemView.findViewById(R.id.address_delete_tv);
        }
    }
}